package br.cin.ufpe.corba.exmaples.complex;

import java.lang.reflect.Constructor;

import br.cin.ufpe.corba.ObjectRef;

public class SimpleCalculatorHelper {
	
	public static SimpleCalculator narrow(final ObjectRef objectRef) {
		if (objectRef == null) {
			return null;
		}

		if (objectRef.is_a(SimpleCalculator.class.getCanonicalName())) {
			try {
				Constructor<?> constructor = SimpleCalculatorStub.class.getConstructor(new Class[] { ObjectRef.class });
				return (SimpleCalculator) constructor.newInstance(new Object[] { objectRef });
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		throw new RuntimeException("Narrow failed");
	}
}
